local skel = fk.CreateSkill {
  name = "emo__touchi",
}

skel:addEffect("active", {
  anim_type = "control",
  prompt = "#emo__touchi",
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  card_num = 0,
  target_num = 1,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:setEmotion(player, "./packages/moepack/image/anim/emo__touchi")
    local target = effect.tos[1]
    local cards = table.filter(target:getCardIds("h"), function(id)
      local card = Fk:getCardById(id)
      return card.trueName == "peach" or card.trueName == "analeptic"
    end)
    if #cards > 0 then
      local card = Fk:getCardById(table.random(cards))
      if not player:prohibitUse(card) and not (card.trueName == "peach" and not player:isWounded()) then
        room:useCard{from = player, tos = {player}, card = card}
      else
        room:moveCardTo(card, Card.Void, nil, fk.ReasonJustMove, skel.name, "", true)
      end
    else
      if #player:getTableMark("@emo__touchi_from") == 0 then
        local skills = table.map(table.filter(target.player_skills, function (s)
          return s:isPlayerSkill(target) and s.visible
        end), Util.NameMapper)
        if #skills == 0 then return false end
        local choice = room:askToChoice(player, {
          choices = skills, skill_name = skel.name, detailed = true,
          prompt = "#emo__touchi-skill:" .. target.id,
        })

        room:addTableMark(target, "@emo__touchi_tar", choice)
        room:addTableMark(player, "emo__touchi_info", {target.id, choice})
        room:handleAddLoseSkills(target, "-"..choice)

        if player:hasSkill(choice, true) then return false end
        room:addTableMark(player, "@emo__touchi_from", choice)
        room:handleAddLoseSkills(player, choice)
      end
    end
  end,
})

skel:addEffect(fk.TargetConfirmed, {
  is_delay_effect = true,
  anim_type = "negative",
  priority = 0.99,
  can_trigger = function(self, event, target, player, data)
    if target == player and data.from and data.from ~= player then
      return table.find(player:getTableMark("emo__touchi_info"), function (mark)
        return mark[1] == data.from.id
      end) ~= nil
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = data.from
    local skills = {}
    local mark = player:getTableMark("emo__touchi_info")
    for i = #mark, 1, -1 do
      local info = mark[i]
      if info[1] == from.id then
        table.remove(mark, i)
        table.insert(skills, info[2])
      end
    end
    if #skills == 0 then return end
    room:setPlayerMark(player, "emo__touchi_info", mark)

    for _, skill in ipairs(skills) do
      if room:removeTableMark(player, "@emo__touchi_from", skill) then
        room:handleAddLoseSkills(player, "-"..skill)
      end
      room:removeTableMark(from, "@emo__touchi_tar", skill)
      room:handleAddLoseSkills(from, skill)
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__touchi_from", 0)
  player.room:setPlayerMark(player, "emo__touchi_info", 0)
  -- 吃掉的技能不会还回去
end)

Fk:loadTranslationTable{
  ["emo__touchi"] = "偷吃",
  [":emo__touchi"] = "出牌阶段限一次，你可以吃掉一名其他角色手牌中一张【桃】或【酒】，若你未吃到，且你未吃有技能，改为吃掉其一个技能，直到你成为其使用牌的目标。",
  ["#emo__touchi"] = "偷吃，选择一名其他角色，吃掉其的一张【桃】或【酒】，若未吃到，则吃掉其1个技能！",
  ["#emo__touchi-skill"] = "偷吃：吃掉 %src 一个技能！",
  ["@emo__touchi_tar"] = "被偷吃",
  ["@emo__touchi_from"] = "偷吃",

  ["$emo__touchi1"] = "（高兴）",
}


return skel
